package com.ruoyi.community.service.message;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.community.domain.MessageAnnounce;
import com.ruoyi.community.domain.MessageAnnounceUser;
import com.ruoyi.community.domain.MessageRemind;
import com.ruoyi.community.mapper.MessageAnnounceMapper;
import com.ruoyi.community.mapper.MessageAnnounceUserMapper;
import com.ruoyi.community.mapper.MessageRemindMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class MessageService {

    @Autowired
    private MessageAnnounceMapper messageAnnounceMapper;

    @Autowired
    private MessageAnnounceUserMapper messageAnnounceUserMapper;

    @Autowired
    private MessageRemindMapper messageRemindMapper;

    /**
     * 新增公告-社区内全体人员
     */
    public String addMessageAnnounce(MessageAnnounce messageAnnounce)
    {
        messageAnnounce.setCommunityId(SecurityUtils.getCommunityId());
        messageAnnounce.setTarget(0);
        messageAnnounce.setCreateTime(DateUtils.getNowDate());
        messageAnnounce.setDeleted(0);
        Long result = messageAnnounceMapper.insertMessageAnnounce(messageAnnounce);
        if(result > 0) {
            return "发送成功";
        }
        return "发送失败";
    }

    /**
     * 新增公告-指定List
     */
    public String addAnnounceByIds(MessageAnnounce messageAnnounce, List<Long> ids) {
        int communityId = SecurityUtils.getCommunityId();
        // 向公告表中插入一条数据
        messageAnnounce.setCommunityId(communityId);
        messageAnnounce.setTarget(1);
        messageAnnounce.setCreateTime(DateUtils.getNowDate());
        messageAnnounce.setDeleted(0);
        messageAnnounceMapper.insertMessageAnnounce(messageAnnounce);

        // 构建插入公告用户表数据
        List<MessageAnnounceUser> announceUserList = new ArrayList<>(ids.size());
//        Date insertDate = new Date();
        for(Long id : ids) {
            announceUserList.add(new MessageAnnounceUser(communityId, messageAnnounce.getId(), id, 0, messageAnnounce.getCreateTime()));
        }
        // 向公告用户表中插入新的记录
        int result = messageAnnounceUserMapper.insertAnnounceUserList(announceUserList);
        if(result > 0) {
            return "发送成功";
        }
        return "发送失败";
    }

    /**
     * 统计未读公告条数
     */
    public int getUnreadAnnounceCount() {
        Long userId = SecurityUtils.getUserID();
        int communityId = SecurityUtils.getCommunityId();

        // 查发送给全体成员的消息
        List<Long> announceIds = messageAnnounceMapper.getLatestAnnounce(communityId, userId);
        System.out.println(announceIds);
        // 如果有的话，向公告用户表中插入消息
        if(announceIds.size() > 0) {
            List<MessageAnnounceUser> announceUserList = new ArrayList<>(announceIds.size());
            for(Long id : announceIds) {
                announceUserList.add(new MessageAnnounceUser(communityId, id, userId, 0, new Date()));
            }
            int result = messageAnnounceUserMapper.insertAnnounceUserList(announceUserList);
        }

        // 根据用户id在公告用户表中查未读消息
        int count = messageAnnounceUserMapper.getUnreadAnnounceCount(communityId, userId);
        return count;
    }

    /**
     * 查询公告列表，按公告发送先后排列，包括未读和已读
     */
    public List<MessageAnnounceUser> getAnnounceList(int page) {
        int size = 20;
        // 根据用户id查未读公告
        List<MessageAnnounceUser> announceList = messageAnnounceUserMapper.selectAnnounceListPage(SecurityUtils.getCommunityId(), SecurityUtils.getUserID(), (page -1) * size , page * size - 1);
        return announceList;
    }

    /**
     * 查询历史公告列表
     */
//    public List<MessageAnnounceUser> getHistoryAnnounceList(int page) {
//        int communityId = SecurityUtils.getCommunityId();
//        Long userId = SecurityUtils.getUserID();
//
//        int size = 10;
//        // 联合查询，返回前10条
//        List<MessageAnnounceUser> historyAnnounceList = messageAnnounceUserMapper.selectHistoryAnnounceList(communityId, userId, page * size , (page + 1) * size - 1);
//        return historyAnnounceList;
//    }

    /**
     * 根据id查指定公告内容，并更新未读状态
     */
    public MessageAnnounce getAnnounceAndUpdateStatus(MessageAnnounceUser messageAnnounceUser) {
        // 查询指定公告内容
        MessageAnnounce announce = messageAnnounceMapper.selectMessageAnnounceById(messageAnnounceUser.getAnnounceId());
        // 更新未读状态
        if(messageAnnounceUser.getStatus() == 0) {
            messageAnnounceUser.setReceiveId(SecurityUtils.getUserID());
            messageAnnounceUser.setCommunityId(SecurityUtils.getCommunityId());
            messageAnnounceUser.setReadTime(new Date());
            int result = messageAnnounceUserMapper.updateStatus(messageAnnounceUser);
        }
        return announce;
    }

    /**
     * 新增提醒，单个人
     * 提醒是一对一，需要提供发布者和接受者信息，其中发布者可以是系统
     */
    public String addRemind(MessageRemind remind) {
        remind.setCommunityId(SecurityUtils.getCommunityId());
        remind.setStatus(0);
        remind.setCreateTime(new Date());
        int result = messageRemindMapper.insertMessageRemind(remind);
        if(result > 0) {
            return "发送成功";
        }
        return "发送失败";
    }

    /**
     * 新增提醒，对多个人发出提醒，提醒内容可能相同或不同
     */
    public String addRemindList(List<MessageRemind> list) {
        int result = messageRemindMapper.insertRemindBatch(list);
        if(result > 0) {
            return "发送成功";
        }
        return "发送失败";
    }

    /**
     * 统计未读提醒条数
     */
    public int getUnreadRemindCount() {
       int count =  messageRemindMapper.getUnreadRemindCount(SecurityUtils.getCommunityId(), SecurityUtils.getUserID());
       return count;
    }

    /**
     * 分页查询提醒列表
     * 按消息发布先后顺序排列，包含未读和已读
     */
    public List<MessageRemind> getRemindList(int page) {
        int size = 20;
        List<MessageRemind> remindList = messageRemindMapper.selectRemindListPage(SecurityUtils.getCommunityId(), SecurityUtils.getUserID(), (page -1) * size , page * size - 1);
        return remindList;
    }

    /**
     * 根据id查询提醒内容，并更新提醒状态
     */
    public MessageRemind getRemindAndUpdateRemind(MessageRemind messageRemind) {
        if(messageRemind.getStatus() == 0) {
            messageRemind.setStatus(1);
            messageRemind.setReadTime(new Date());
            int result = messageRemindMapper.updateMessageRemind(messageRemind);
        }
        MessageRemind remind = messageRemindMapper.selectMessageRemindById(messageRemind.getId());
        return remind;
    }
}